using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace EntLibEC.Dac.Online
{
    public class SearchKeywordDac
    {
        public static void Insert(string keyword, int priority, int isOnlineShow, int createUserSysNo, string customerIP)
        {
            string strInsert = @"Insert into Ent_SearchKeyword(Keyword, Priority, IsOnlineShow, CreateUserSysNo, CustomerIP) 
                values (@Keyword, @Priority, @IsOnlineShow, @CreateUserSysNo, @CustomerIP)";

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strInsert);

            db.AddInParameter(dbCommand, "Keyword", DbType.String, keyword);
            db.AddInParameter(dbCommand, "Priority", DbType.Int32, priority);
            db.AddInParameter(dbCommand, "IsOnlineShow", DbType.Int32, isOnlineShow);
            db.AddInParameter(dbCommand, "CreateUserSysNo", DbType.Int32, createUserSysNo);
            db.AddInParameter(dbCommand, "CustomerIP", DbType.String, customerIP);

            db.ExecuteNonQuery(dbCommand);
        }

        public static DataSet Query(DateTime? fromDateTime, DateTime? toDateTime)
        {
            string strSelect = "Select * From Ent_SearchKeyword with (nolock) ";

            if (fromDateTime.HasValue && toDateTime.HasValue)
            {
                strSelect += " where CreateDate >='@FromDateTime' and CreateDate<='@ToDateTime' ";
                strSelect = strSelect.Replace("@FromDateTime", fromDateTime.Value.ToString(EntLibEC.Cmn.AppConst.DateFormat));
                strSelect = strSelect.Replace("@ToDateTime", toDateTime.Value.ToString(EntLibEC.Cmn.AppConst.DateFormat));
            }
            if (fromDateTime == null && toDateTime.HasValue)
            {
                strSelect += " where CreateDate<='@ToDateTime' ";
                strSelect = strSelect.Replace("@ToDateTime", toDateTime.Value.ToString(EntLibEC.Cmn.AppConst.DateFormat));
            }
            if (fromDateTime.HasValue && toDateTime == null)
            {
                strSelect += " where CreateDate >='@FromDateTime' ";
                strSelect = strSelect.Replace("@FromDateTime", fromDateTime.Value.ToString(EntLibEC.Cmn.AppConst.DateFormat));
            }

            strSelect += " Order By SysNo Desc ";

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSelect);

            return db.ExecuteDataSet(dbCommand);
        }
    }
}
